package com.pkutech.carcasher.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.pkutech.carcasher.R;
import com.pkutech.carcasher.content.MessageAdapter;
import com.pkutech.carcasher.content.MessageDataSource;
import com.pkutech.carcasher.db.RealmManager;
import com.pkutech.carcasher.model.Message;
import com.pkutech.carcasher.utils.ToastUtil;
import com.shizhefei.mvc.MVCHelper;
import com.shizhefei.mvc.MVCSwipeRefreshHelper;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
 * Created by cyy on 2016/6/10.
 */
public class MsgManagedActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

    private MVCHelper<List<Message>> mvcHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg_managed);
        ButterKnife.bind(this);
        initToolbar(toolbar);

        mvcHelper = new MVCSwipeRefreshHelper<List<Message>>(swipeRefreshLayout);
        // 设置数据源
        mvcHelper.setDataSource(new MessageDataSource());
        // 设置适配器
        mvcHelper.setAdapter(new MessageAdapter(this));

        // 加载数据
        mvcHelper.refresh();

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                new MaterialDialog.Builder(MsgManagedActivity.this)
                        .title(getString(R.string.delete_ue))
                        .items(R.array.delete_ue_lable)
                        .itemsCallback(new MaterialDialog.ListCallback() {
                            @Override
                            public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                                switch (which) {
                                    case 0:
//                                        MessageAdapter.ViewHolder holder = (MessageAdapter.ViewHolder)view.getTag();
                                        MessageDataSource messageDataSource = (MessageDataSource)mvcHelper.getDataSource();
                                        List<Message> messageList = messageDataSource.getMessages();
                                        RealmManager.deleteMessageByTime(messageList.get(which).getTime());
                                        // 加载数据
                                        mvcHelper.refresh();
                                        ToastUtil.showInfoToast(MsgManagedActivity.this, getString(R.string.delete_success), ToastUtil.Position.TOP);
                                        break;
                                }
                            }
                        })
                        .show();

                return false;
            }
        });
    }
}